package com.haixiaoke.saas.enterprise.house.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import com.haixiaoke.saas.common.annotation.Log;
import com.haixiaoke.saas.common.annotation.TenantScope;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.enums.BusinessType;
import com.haixiaoke.saas.common.tenant.TenantContext;
import com.haixiaoke.saas.common.utils.poi.ExcelUtil;
import com.haixiaoke.saas.house.domain.CleaningProcess;
import com.haixiaoke.saas.house.domain.CleaningProcessTree;
import com.haixiaoke.saas.house.service.ICleaningProcessService;
import com.haixiaoke.saas.system.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 保洁流程Controller
 *
 * @author panda
 * @date 2024-05-09
 */
@RestController
@RequestMapping("/enterprise/house/cleaningProcess")
public class EnterpriseCleaningProcessController extends BaseController {
    @Autowired
    private ICleaningProcessService cleaningProcessService;

    /**
     * 查询保洁流程列表
     */
    @PreAuthorize("@ss.hasPermi('enterprise:cleaningProcess:list')")
    @GetMapping("/list")
    @TenantScope
    public TableDataInfo list(CleaningProcess cleaningProcess) {
        startPage();
        cleaningProcess.setTenantId(TenantContext.getTenantId());
        List<CleaningProcess> list = cleaningProcessService.selectHouseListByCleaningProcess(cleaningProcess);
        return getDataTable(list);
    }

     /**
     * 查询保洁流程列表
     */
     @PreAuthorize("@ss.hasPermi('enterprise:cleaningProcess:tree')")
     @GetMapping("/tree")
    @TenantScope
    public AjaxResult tree(CleaningProcess cleaningProcess) {
        cleaningProcess.setTenantId(TenantContext.getTenantId());
        List<CleaningProcessTree> list = cleaningProcessService.selectCleaningProcessTree(cleaningProcess);
        return success(list);
    }

    /**
     * 导出保洁流程列表
     */
    @PreAuthorize("@ss.hasPermi('enterprise:cleaningProcess:export')")
    @Log(title = "保洁流程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CleaningProcess cleaningProcess) {
        List<CleaningProcess> list = cleaningProcessService.selectCleaningProcessList(cleaningProcess);
        ExcelUtil<CleaningProcess> util = new ExcelUtil<CleaningProcess>(CleaningProcess.class);
        util.exportExcel(response, list, "保洁流程数据");
    }

    /**
     * 获取保洁流程详细信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:cleaningProcess:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(cleaningProcessService.selectCleaningProcessById(id));
    }

    /**
     * 新增保洁流程--位置
     */
    @PreAuthorize("@ss.hasPermi('enterprise:cleaningProcess:add')")
    @Log(title = "保洁流程", businessType = BusinessType.INSERT)
    @PostMapping("/add/place")
    @TenantScope
    public AjaxResult addPlace(@RequestBody CleaningProcess cleaningProcess) {
        cleaningProcess.setParentId(null);
        cleaningProcess.setTenantId(TenantContext.getTenantId());
        cleaningProcess.setCreateBy(getUsername());
        return toAjax(cleaningProcessService.insertCleaningProcessByPlace(cleaningProcess));
    }

    /**
     * 新增保洁流程--检查点
     */
     @PreAuthorize("@ss.hasPermi('enterprise:cleaningProcess:add')")
    @Log(title = "保洁流程", businessType = BusinessType.INSERT)
    @PostMapping("/add/checkPoint")
    @TenantScope
    public AjaxResult addCheckPoint(@RequestBody CleaningProcess cleaningProcess) {
        if (cleaningProcess.getParentId() == null) {
            return error("请选择位置");
        }
        CleaningProcess parentProcess = cleaningProcessService.selectCleaningProcessById(cleaningProcess.getParentId());
        if (ObjectUtil.isEmpty(parentProcess)) {
            return error("父节点不存在");
        }
        if (parentProcess.getParentId()!=null) {
            return error("检查点不能添加检查点");
        }
        cleaningProcess.setTenantId(TenantContext.getTenantId());
        cleaningProcess.setCreateBy(getUsername());
        return toAjax(cleaningProcessService.insertCleaningProcessByCheckPoint(cleaningProcess));
    }

    /**
     * 修改保洁流程
     */
    @PreAuthorize("@ss.hasPermi('enterprise:cleaningProcess:edit')")
    @Log(title = "保洁流程", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CleaningProcess cleaningProcess) {
        CleaningProcess process = new CleaningProcess();
        process.setId(cleaningProcess.getId());
        process.setCleaningProcessName(cleaningProcess.getCleaningProcessName());
        process.setUpdateBy(getUsername());
        return toAjax(cleaningProcessService.updateCleaningProcess(process));
    }

    /**
     * 修改保洁流程排序上移
     */
    @PreAuthorize("@ss.hasPermi('enterprise:cleaningProcess:edit')")
    @Log(title = "保洁流程", businessType = BusinessType.UPDATE)
    @PutMapping("/edit/moveUp/{id}")
    @TenantScope
    public AjaxResult editMoveUp(@PathVariable("id") Long id) {
        return toAjax(cleaningProcessService.cleaningProcessMoveUp(id, TenantContext.getTenantId()));
    }

    /**
     * 修改保洁流程排序下移
     */
    @PreAuthorize("@ss.hasPermi('enterprise:cleaningProcess:edit')")
    @Log(title = "保洁流程", businessType = BusinessType.UPDATE)
    @PutMapping("/edit/shiftDown/{id}")
    @TenantScope
    public AjaxResult editShiftDown(@PathVariable("id") Long id) {
        return toAjax(cleaningProcessService.cleaningProcessShiftDown(id, TenantContext.getTenantId()));
    }

    /**
     * 删除保洁流程
     */
    @PreAuthorize("@ss.hasPermi('enterprise:cleaningProcess:remove')")
    @Log(title = "保洁流程", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    @TenantScope
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(cleaningProcessService.deleteCleaningProcessById(id,TenantContext.getTenantId()));
    }
}
